package evs1.restframework.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;

import evs1.restframework.model.Company;
import evs1.restframework.model.Occupation;
import evs1.restframework.model.Person;
import evs1.restframework.model.Skill;

public class TestClient 
{
	private XStream xmlParser;
	private XStream jsonParser;
	//private String baseUri = "http://localhost:8080/evs1_0726559/";
	private String baseUri = "http://localhost:8080/evs11/";
	
	public TestClient()
	{
		xmlParser = new XStream(new DomDriver());
		jsonParser = new XStream(new JsonHierarchicalStreamDriver());			
		
		Person p1 = new Person();
		p1.setPrename("Thomas");
		p1.setSurname("Meier");
		
		Skill s1 = new Skill();
		s1.setName("English");
		s1.setPerson(p1);
		
		Skill s2 = new Skill();
		s2.setName("German");
		s2.setPerson(p1);
		
		HashSet<Skill> skills1 = new HashSet<Skill>();
		skills1.add(s1);
		skills1.add(s2);
		
		p1.setSkills(skills1);
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		Company c = new Company();
		c.setName("google");
		try {
			c.setFounded(sdf.parse("10.12.2000"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setFieldOfActivity("everything");
		
		Skill s3 = new Skill();
		s3.setName("English");
		s3.setCompany(c);
		
		Skill s4 = new Skill();
		s4.setName("French");
		s4.setCompany(c);
		
		HashSet<Skill> skills2 = new HashSet<Skill>();
		skills2.add(s3);
		skills2.add(s4);
		
		c.setSkills(skills2);
		
		Occupation o = new Occupation();
		try {
			o.setBegin(sdf.parse("10.12.2005"));
			o.setEnd(sdf.parse("12.11.2009"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		o.setPosition("CEO");
		o.setCompany(c);
		o.setPerson(p1);
		
		HashSet<Occupation> occupations = new HashSet<Occupation>();
		occupations.add(o);
		p1.setOccupations(occupations);
		c.setOccupations(occupations);
		
		testPost(baseUri + "persons", "application/xml", p1);
		testPost(baseUri + "companies", "application/xml", c);
		
		testGet(baseUri + "persons/1", "application/json");		
		testGet(baseUri + "companies/1", "application/xml");
		testGet(baseUri + "occupations/1", "application/json");
		
		testDelete(baseUri + "persons/1", "application/xml");
		testDelete(baseUri + "companies/1", "application/json");
		testDelete(baseUri + "occupations/1", "application/json");
	}
	public static void main(String[] args)
	{
		new TestClient();
		System.exit(0);
	}
	/**
	 * method for testing get
	 * @param uri
	 * @param contentType
	 */
	private void testGet(String uri, String contentType)
	{
		System.out.println("\nGetting object:");
		try {
			URL url = new URL(uri);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-Type", contentType);
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			String str;
			while((str = in.readLine()) != null )
				System.out.println(str);
			in.close();

		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void testPost(String uri, String contentType, Object obj) 
	{
		System.out.println("\nPosting object. Return:");
		
		try {
			String objStr = "";

			if(contentType.equals("application/json")) {
				objStr = jsonParser.toXML(obj);
			} else {
				objStr = xmlParser.toXML(obj);
			}
			System.out.println(objStr);

			URL url = new URL(uri);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", contentType);
			conn.setDoOutput(true);

			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
			out.write(objStr, 0, objStr.length());
			out.close();

			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String str;
			while((str = in.readLine()) != null )
				System.out.println(str);
			in.close();

		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void testDelete(String uri, String contentType)
	{
		System.out.println("\nDeleting object:");
		try {
			URL url = new URL(uri);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("DELETE");
			conn.setRequestProperty("Content-Type", contentType);
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			String str;
			while((str = in.readLine()) != null )
				System.out.println(str);
			in.close();

		} catch(Exception e) {
			System.out.println(e);
		}
	}
}
